    var form = layui.form, table = layui.table, $ = layui.jquery, id = getQueryVariable("id");

//表单初始值，修改时调用
    if (id != "") {
        form.val("editor-form", {
            "emptynumber": "置空单号",
        })
    }
    $('#editor-form').find('input,textarea,select').attr('disabled',true);
let detailData = [];
let childData = [];
let annexList = [];
let operationLog = [];
layer.ready(function () {
    //获取详情信息
    $.ajax({
        type:"POST",
        dataType:"json",
        contentType: 'application/json',
        data:JSON.stringify(id),
        url:"transfer/detail/getTransferDetailInfo",
        success:function(res){
            if(res.status == 200){
                //主表信息赋值
                $("#billNo").val(res.data.billNo);
                $("#time").val(res.data.time);
                $("#type").val(res.data.typeStr);
                $("#warehouseName").val(res.data.warehouseName);
                $("#founder").val(res.data.creator);
                $("#createDate").val(res.data.createDate);
                $("#memo").val(res.data.memo);

                //产品列表信息
                detailData = res.data.productVos;

                //附件信息
                annexList = res.data.annexVos;

                table.reload('storageproductstableid',{data : detailData});
                table.reload('attachmenttableid',{data : annexList});

                return false;
            }else {
                layer.msg(res.msg, {icon: 5});
            }
        }
    });
})
table.render({
        elem: '#storageproducts',
        id: 'storageproductstableid',
        data: detailData,
        toolbar: '#storageproductstoolbar',
        cols: [[
            {field: 'inStoreBillNo', width: 120, title: '入库单号', sort: true},
            {field: 'productCode', width: 120, title: '产品编码', sort: true},
            {field: 'lotNo', width: 120, title: '批次号', sort: true},
            {field: 'warehouseName', width: 120, title: '仓库名称', sort: true},
            {field: 'newWarehouseName',width: 120,title: '新仓库名称'},
            {field: 'specificationCode', width: 120, title: '规格编码', sort: true},
            {field: 'contractNo', width: 120, title: '销售合同号', sort: true},
            {field: 'productUnit', width: 120, title: '产品单位', sort: true},
            {field: 'newContractNo', width: 120, title: '新合同号'},
            {field: 'transferQuantity', width: 120, title: '调拨数量'},
            {field: 'stockQuantity', width: 120, title: '库存数量', sort: true},
            {field: 'canTransferQuantity', width: 140, title: '允许调拨数量', sort: true},
            {field: 'salesUnitPrice', width: 120, title: '销售单价'},
            {field: 'money', width: 120, title: '金额小计'},
            {field: 'color', width: 120, title: '颜色'},
            {field: 'costUnitPrice', width: 120, title: '成本单价'},
            {field: 'shareCostPrice', width: 160, title: '分摊费用单价'},
            {field: 'trayNumber', width: 120, title: '托盘入数', sort: true},
            {field: 'totalTray', width: 120, title: '总托盘', sort: true},
            {field: 'clientProductNo', width: 120, title: '客户货号', sort: true},
            {field: 'productCname', width: 120, title: '中文名称', sort: true},
            {field: 'productEname', width: 120, title: '英文名称', sort: true},
            {field: 'productCdesc', width: 120, title: '中文描述', sort: true},
            {field: 'productEdesc', width: 120, title: '英文描述', sort: true},
            {field: 'specificationExplanation', width: 120, title: '规格说明', sort: true},
            {field: 'memo', width:220, title: '备注', sort: true}
        ]],
        page: true,
        height: 380
        ,done: function (res, curr, count) {
            table.render({
                elem: '#thedetail'
                ,data:childData
                ,cols: [[
                    {field: 'inStoreBillNo', title: '入库单号', sort: true},
                    {field: 'costUnitPrice', title: '入库单价', sort: true},
                    {field: 'canTransferQuantity', title: '可调拨数量', sort: true},
                    {field: 'transferQuantity', title: '调拨数量'},
                ]]
                ,id: 'thedetailtableid'
                ,where: {
                    id:res.data.length == 0 ? "":res.data[0].id //默认第一条信息，数据为空则id为空
                }
                ,height: 200
            });
        }
    });

    //单击行事件
    table.on('row(storageproductstable)', function (obj) {
        let tData = dataCopy(obj.data);
        table.reload('thedetailtableid',{data : tData});
    });

    //字段赋值
    function dataCopy(data){
        let tData = [{
            stockId : data.stockId,
            inStoreBillNo : data.inStoreBillNo,
            costUnitPrice : data.costUnitPrice,
            canTransferQuantity : data.canTransferQuantity,
            transferQuantity : data.transferQuantity
        }];
        return tData;
    }

    //---附件
    table.render({
        elem: '#attachment'
        , data: annexList
        , toolbar: '#attachmenttoolbar'
        , cols: [[
            {field: 'fileName', title: '文件名', fixed: true}
            , {field: 'uploadTime', title: '上传日期', fixed: true}
            , {fixed: 'right', width: 180, align: 'center', toolbar: '#attachmenteditor', title: '操作'}
        ]]
        , id: 'attachmenttableid'
        , height: 380
        , page: true
    });

    table.on('tool(attachmenttable)', function (obj) {
        let data = obj.data;
        if (obj.event === 'download') { //下载
            let xmlHttpRequest = new XMLHttpRequest();
            let url = "wjinfo/downloadfile2?filename=" + data.filePath;
            xmlHttpRequest.open('GET', url, true);
            //设置请求头
            xmlHttpRequest.setRequestHeader("Content-type", "application/json")
            xmlHttpRequest.responseType = "blob";
            xmlHttpRequest.send(null);

            xmlHttpRequest.onload = function() {
                let content = xmlHttpRequest.response;
                // 组装a标签
                let elink = document.createElement("a");

                //设置文件下载路径
                elink.download = decodeURIComponent(data.fileName);
                elink.style.display = "none";
                let blob = new Blob([content], { type: 'application/force-download' });

                //解决下载不存在文件的问题，根据blob大小判断
                if(blob.size==0){
                    layer.msg('服务器没找到此文件，请联系管理员!');
                }else{
                    elink.href = window.URL.createObjectURL(blob);
                    document.body.appendChild(elink);
                    elink.click();
                    document.body.removeChild(elink);
                }

            }
        }
    });

//---操作日志
    table.render({
        elem: '#otherwarehouselog'
        , data: operationLog
        , cols: [[
            {field: 'operationtype', title: '操作类型', fixed: true}
            , {field: 'theoperator', title: '操作员'}
            , {field: 'operationdate', title: '操作日期'}
            , {field: 'modifythecontent', title: '修改内容'}
        ]]
        , id: 'logtableid'
        , height: 380
        , limit: 30//显示的数量
        , page: true
    });